package com.zr.controller;


import com.zr.dto.UserDTO;
import com.zr.enums.CoreExceptionEnum;
import com.zr.exception.ServiceException;
import com.zr.pojo.Result;
import com.zr.service.AuthService;
import com.zr.utils.JsonUtils;
import com.zr.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.Locale;

/**
 * @author: peterChen
 * @date: 2019/9/22 19:14
 * @description:
 * @version: 1.0
 */
@Slf4j
@RestController
public class AuthController {

    @Autowired
    private AuthService authService;


   // /**
   //  * 登录
   //  * @param userVO
   //  *
   //  * @return
   //  */
   //@RequestMapping("/login")
   //public ResponseEntity<Void> login(
   //        @RequestBody UserVO userVO
   //){
   //    authService.login(userVO);
   //    return ResponseEntity.ok().build();
   //}
    /**
     * 注册用户
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("register")
    @PreAuthorize("hasRole('admin')")
    public ResponseEntity<Void> register(@RequestParam("username")String username,
                                         @RequestParam("password")String password){
        authService.register(username,password);
        //request.getSession().setAttribute("username",username);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 获取登录用户的角色信息
     * @param
     * @return
     */
    //@RequestMapping("getInfo")
    //@PreAuthorize("isAuthenticated()")
    //public Result getRoleByActiveUser(HttpServletRequest request, Authentication authentication){

       // SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession()
       //         .getAttribute("SPRING_SECURITY_CONTEXT");
       //     String username = securityContextImpl.getAuthentication().getName();
       // if( StringUtils.isEmpty(username)){
       //     return new Result("400","用户未登录",null);
       // }
       // UserDTO userDTO = authService.selectDtoByLoginName(username);
       // // UserDTO userDTO = authService.selectDtoByLoginName("admin");
       // return new Result("200","获取用户角色成功！",userDTO.getRoles());
       //Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
       //if (principal != null && principal instanceof UserDetails) {
       //    UserDetails user = (UserDetails) principal;
       //    return new Result("200","获取登录用户信息成功",user);
       //}
       //return new Result("400","获取登录用户信息失败",null);
       // Principal userPrincipal = request.getUserPrincipal();
       // if (userPrincipal != null && userPrincipal instanceof UserDetails) {
       //     UserDetails user = (UserDetails) userPrincipal;
       //     return new Result("200", "获取登录用户信息成功", user);
       // }
       // return new Result("400","获取登录用户信息失败",null);

      //Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      //if (principal != null && principal instanceof UserDetails) {
      //   return new Result("200","获取登录信息成功",principal);
      //}
      //if (principal != null && principal instanceof Principal) {
      //    return new Result("200","获取登录信息成功",principal);
      //}
      //log.error("未登录");
      //return new Result("401","未登录",null);

        //SecurityContextImpl securityContextImpl = (SecurityContextImpl)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        //if(securityContextImpl == null){
        //    return new Result("401","用户未登录",null);
        //}
        //String username = securityContextImpl.getAuthentication().getName();
        //UserDTO userDTO = authService.selectDtoByLoginName(username);
        ////UserDTO userDTO = authService.selectDtoByLoginName("admin");
        //return new Result("200","获取用户角色成功！",userDTO.getRoles());
    //}
}
